/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.supplier.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import com.pitop.supplier.api.SupplierCategoryFeignApi;
import com.pitop.core.exceptions.SupplierCategoryException;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.entity.Page;
import com.pitop.supplier.entity.SupplierCategory;


/**
 * 供应商类型
 *
 * @author berton
 */
@Service("supplierCategorySVImpl")
@Slf4j
public class SupplierCategorySVImpl {

    @Autowired
    private SupplierCategoryFeignApi supplierCategoryFeignApi;

    /**
     * 加载一个对象SupplierCategory
     *
     * @param id * @param code 类型编码
     * @return SupplierCategory
     */
    public SupplierCategory load(Long id, String code) {
        if (id == null && code == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return supplierCategoryFeignApi.load(id, code);
    }

    /**
     * 加载一个对象SupplierCategory 通过id
     *
     * @param id
     * @return SupplierCategory
     */
    public SupplierCategory loadById(Long id) {
        if (id == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return supplierCategoryFeignApi.loadById(id);
    }

    /**
     * 加载一个对象SupplierCategory 通过code
     *
     * @param code 类型编码
     * @return SupplierCategory
     */
    public SupplierCategory loadByCode(String code) {
        if (code == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return supplierCategoryFeignApi.loadByCode(code);
    }


    /**
     * 删除对象SupplierCategory
     *
     * @param id * @param code 类型编码
     * @return SupplierCategory
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        supplierCategoryFeignApi.delete(id, code);
    }


    /**
     * 查询SupplierCategory分页
     *
     * @param supplierCategory 对象
     * @param curPage          当前页
     * @param pageSize         分页条数
     * @return List<SupplierCategory>
     */
    public List<SupplierCategory> list(SupplierCategory supplierCategory, int curPage, int pageSize) {
        return supplierCategoryFeignApi.list(supplierCategory, curPage, pageSize);
    }

    /**
     * 查询SupplierCategory分页
     *
     * @param id       * @param code  类型编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<SupplierCategory>
     */
    public List<SupplierCategory> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        SupplierCategory supplierCategory = new SupplierCategory();
        supplierCategory.setId(id);
        supplierCategory.setCode(code);

        return supplierCategoryFeignApi.list(supplierCategory, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return supplierCategoryFeignApi.count(id, code);
    }


    public int count(SupplierCategory supplierCategory) {
        return supplierCategoryFeignApi.count(supplierCategory);
    }


    /**
     * 保存
     * 1.数据合法性校验
     * ****非空校验
     * ****供应商类型名称是否已经被使用
     * 2.保存供应商类型
     *
     * @param supplierCategory 实体
     * @throws BaseException
     */
    public void save(SupplierCategory supplierCategory) {
        //1.数据合法性校验
        if (supplierCategory == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应商类型名称是否已经被使用
        SupplierCategory supplierCategoryFlag = supplierCategoryFeignApi.loadByName(supplierCategory.getName());
        if (supplierCategoryFlag != null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.保存供应商类型
        supplierCategoryFeignApi.build(supplierCategory);
    }

    /**
     * 更新
     * 1.数据合法性校验
     * ****非空校验
     * ****供应商类型名称是否已经被使用
     * 2.更新供应商类型
     *
     * @param supplierCategory 实体
     * @throws BaseException
     */
    public void modify(SupplierCategory supplierCategory) {
        //1.数据合法性校验
        if (supplierCategory == null) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        //供应商类型名称是否已经被使用
        SupplierCategory supplierCategoryFlag = supplierCategoryFeignApi.loadByName(supplierCategory.getName());
        if (supplierCategoryFlag != null && !supplierCategory.getCode().equals(supplierCategoryFlag.getCode())) {
            throw new SupplierCategoryException(BaseException.BaseExceptionEnum.Exists);
        }
        //2.更新供应商类型
        supplierCategoryFeignApi.modify(supplierCategory);
    }

}
